﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using log4net;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for MultipleChoiceAnswersPage.xaml
    /// </summary>
    public partial class MultipleChoiceAnswersPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MultipleChoiceAnswersPage));
        private readonly CoreConfig Config;
        private readonly MultipleChoiceConfig pConfig;
        private readonly WorkflowCompleter _workflow;

        public MultipleChoiceAnswersPage(string question) {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;
            pConfig = new MultipleChoiceConfig() { Question = question };
            pConfig.Background = Config.Theme.Background;
            _workflow = new WorkflowCompleter(this);

            //First pass, create five new answers

            _answer1 = new Answer();
            Answer1.DataContext = _answer1;
            CorrectCheckBox1.DataContext = _answer1;

            _answer2 = new Answer();
            Answer2.DataContext = _answer2;
            CorrectCheckBox2.DataContext = _answer2;

            _answer3 = new Answer();
            Answer3.DataContext = _answer3;
            CorrectCheckBox3.DataContext = _answer3;

            _answer4 = new Answer();
            Answer4.DataContext = _answer4;
            CorrectCheckBox4.DataContext = _answer4;

            _answer5 = new Answer();
            Answer5.DataContext = _answer5;
            CorrectCheckBox5.DataContext = _answer5;

            PreviewRoot.Content = PagePreviewFactory.Create(pConfig);
            UpdateAnswers();
        }

        private Answer _answer1;
        private Answer _answer2;
        private Answer _answer3;
        private Answer _answer4;
        private Answer _answer5;

        public void UpdateAnswers() {
            if(pConfig == null) {
                return;
            }

            //TODO: There has to be a better algorithm for this....
            pConfig.Answers.Clear();
            if(string.IsNullOrWhiteSpace(Answer5.Text)) {
                if(string.IsNullOrWhiteSpace(Answer4.Text)) {
                    Answer5.IsEnabled = false;
                    if(string.IsNullOrWhiteSpace(Answer3.Text)) {
                        Answer4.IsEnabled = false;
                        if(string.IsNullOrWhiteSpace(Answer2.Text)) {
                            Answer3.IsEnabled = false;
                            if(string.IsNullOrWhiteSpace(Answer1.Text)) {
                                Answer2.IsEnabled = false;
                                pConfig.Answers.Add(_answer1);
                            } else {
                                Answer2.IsEnabled = true;
                                pConfig.Answers.Add(_answer1);
                            }
                        } else {
                            Answer2.IsEnabled = true;
                            Answer3.IsEnabled = true;
                            pConfig.Answers.Add(_answer1);
                            pConfig.Answers.Add(_answer2);
                        }
                    } else {
                        Answer2.IsEnabled = true;
                        Answer3.IsEnabled = true;
                        Answer4.IsEnabled = true;
                        pConfig.Answers.Add(_answer1);
                        pConfig.Answers.Add(_answer2);
                        pConfig.Answers.Add(_answer3);
                    }
                } else {
                    Answer2.IsEnabled = true;
                    Answer3.IsEnabled = true;
                    Answer4.IsEnabled = true;
                    Answer5.IsEnabled = true;
                    pConfig.Answers.Add(_answer1);
                    pConfig.Answers.Add(_answer2);
                    pConfig.Answers.Add(_answer3);
                    pConfig.Answers.Add(_answer4);
                }
            } else {
                pConfig.Answers.Add(_answer1);
                pConfig.Answers.Add(_answer2);
                pConfig.Answers.Add(_answer3);
                pConfig.Answers.Add(_answer4);
                pConfig.Answers.Add(_answer5);
            }

        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

  
        private void NextButton_Click(object sender, RoutedEventArgs e) {
            //Forward the created config to the assignment page 
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                _workflow.Complete();
                navService.Navigate(new QuestionSectionAssignmentPage(pConfig));
            }
        }

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        //private void AnswerEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) {
        //    UpdateAnswers();
        //}

        private void AnswerTextChanged(object sender, TextChangedEventArgs e) {
            UpdateAnswers();
        }


        
    }
}
